//Tranz-Am
#include "game_transam.h"

//local game specifics
static bool newLevelRequired=true;	//new level

static float mapXoff = 0;		//place in map we are at (top left)
static float mapYoff = 0;

static Car* Player=NULL;
static int countdown=1;
static long secondsPlayed=0;

//logic called 1/FPS seconds
bool TransAmGameLogic()
{
	//has a new level started
	if(newLevelRequired) 
	{
		if(!InitialiseLevel())
			return true;	//exit logic loop and game if failure
	}

	//update relevant in game statistics
	//update counters every second
	if(countdown>=GameConfiguration->CapsSystem.fps)
	{
		countdown=1;
		secondsPlayed++;
	}
	else
		countdown++;

	//update car movement and keyboard handling
	//returns true if exitting (e.g. pressing ESCape)
	if(Player->NextMove()) 
	{
		CleanUp();
		return true;
	}

	//move the map the same as the player has moved
	//the car is always in the centre
	mapYoff=Player->mapy-(ShownMapHeight/2);
	mapXoff=Player->mapx-(ShownMapWidth/2);

	//stop falling off, just in case car code doens't work
	if(mapYoff+ShownMapHeight>(mapblockheight*mapheight-1))
		mapYoff=mapblockheight*mapheight-1-ShownMapHeight;
	if(mapYoff<0) mapYoff=0;
	if(mapXoff+ShownMapWidth>(mapblockwidth*mapwidth-1))
		mapXoff=mapblockwidth*mapwidth-1-mapXoff;
	if(mapXoff<0) mapXoff=0;

	//mappy animations
	MapUpdateAnims();
	
	return false;
}

//drawing loop, called 1/FPS seconds
bool TransAmGameDrawing()
{
	clear(GameFramework->DrawingSurface);

	//score panel - always top right. maybe only show if screen is > certain size
	blit(DisplayPanel->ReadOnly_CurrentItem,GameFramework->DrawingSurface,0,0,0,0,DisplayPanel->ReadOnly_CurrentItem->w,DisplayPanel->ReadOnly_CurrentItem->h);
	//lives
	draw_sprite(GameFramework->DrawingSurface, PanelCars[0]->ReadOnly_CurrentItem, 18, 424);
	draw_sprite(GameFramework->DrawingSurface, PanelCars[1]->ReadOnly_CurrentItem, 60, 424);
	draw_sprite(GameFramework->DrawingSurface, PanelCars[2]->ReadOnly_CurrentItem, 104, 424);

	//dials
	int speedwidth;
	speedwidth=((float)DisplaySpeedo->ReadOnly_CurrentItem->w/Car::MPH_MAX)*((Player->Mph));
	if(speedwidth>DisplaySpeedo->ReadOnly_CurrentItem->w) speedwidth=DisplaySpeedo->ReadOnly_CurrentItem->w;
	blit(DisplaySpeedo->ReadOnly_CurrentItem,GameFramework->DrawingSurface,0,0,58,296,speedwidth,DisplaySpeedo->ReadOnly_CurrentItem->h);

	int tempwidth;
	tempwidth=(int)(((float)DisplayTemperature->ReadOnly_CurrentItem->w/100)*((Player->Temperature)));
	if(tempwidth>DisplayTemperature->ReadOnly_CurrentItem->w) tempwidth=DisplayTemperature->ReadOnly_CurrentItem->w;
	blit(DisplayTemperature->ReadOnly_CurrentItem,GameFramework->DrawingSurface,0,0,58,376,tempwidth,DisplayTemperature->ReadOnly_CurrentItem->h);

	float Fuelwidth;
	Fuelwidth=((float)(DisplayFuel->ReadOnly_CurrentItem->w/Car::FUEL_MAX))*Player->Fuel;	//60 as that is how long fuel lasts
	if((int)Fuelwidth>DisplayFuel->ReadOnly_CurrentItem->w) Fuelwidth=(float)DisplayFuel->ReadOnly_CurrentItem->w;
	blit(DisplayFuel->ReadOnly_CurrentItem,GameFramework->DrawingSurface,0,0,58,336,(int)Fuelwidth,DisplayFuel->ReadOnly_CurrentItem->h);

	//time and distance
	textprintf_ex(GameFramework->DrawingSurface,font,35,35,makecol(255,255,255),-1,"%03ld:%02ld",secondsPlayed/60,secondsPlayed%60);	
	textprintf_ex(GameFramework->DrawingSurface,font,35,63,makecol(255,255,255),-1,"%06.0f",Player->MilesDriven);	

	//mappy function to draw the relevant layers
	//draw to the location set up at the start (to the right of the panel)
	//draw to fill the remainder of the screen
	//set starting point within the map to show - at the start it is the approximate centre

	//order is background first, then player car, then any foreground stuff
	//draw map background
	MapDrawBGT (GameFramework->DrawingSurface, (int)mapXoff, (int)mapYoff, ScreenXOffset, ScreenYOffset, ShownMapWidth, ShownMapHeight);

	//car
	if(Player->car) rotate_sprite(GameFramework->DrawingSurface, Player->car->ReadOnly_CurrentItem, Player->screenx, Player->screeny, itofix(FIX_ANGLE[Player->angle]));

	//draw foreground stuff
	MapDrawFG (GameFramework->DrawingSurface, (int)mapXoff, (int)mapYoff, ScreenXOffset, ScreenYOffset, ShownMapWidth, ShownMapHeight, 0);
	textprintf_ex(GameFramework->DrawingSurface,font,0,0,makecol(255,255,255),0,"maxw %d maxh %d angle %d vel %f mph %d FPS %d",mapblockwidth*mapwidth-1,mapblockheight*mapheight,Player->angle,Player->vel,Player->Mph,TimerCurrentFPS);
	textprintf_ex(GameFramework->DrawingSurface,font,180,10,makecol(255,255,255),0,"Version 4: car and stuff. Cursors move. Esc exit.");
	
	return false;
}

/**********************************************
	local helper functions
*/

//initialise the new level
bool InitialiseLevel()
{
	newLevelRequired=false;
	secondsPlayed=0;
	
	if(!LoadLevel())		//load map
	{
		Configuration::GlobalErrorString="Could not initialise map. Check log file and the transam.fmp file exists.";
		return false;
	}
	//initialise the location of what part of the map (top left) we show - the middle
	mapXoff=(mapblockwidth*mapwidth)/2;
	mapYoff=(mapblockheight*mapheight)/2;
	
	//initialise the car
	Player->SetupCar();

	//initialise the car location to be the current map location, but in the centre
	Player->mapx=mapXoff+(ShownMapWidth/2);
	Player->mapy=mapYoff+(ShownMapHeight/2);
	
	return true;
}

//reset everything
void CleanUp()
{
	//player
	if(!Player) delete Player;
	Player=NULL;

	//mappy
	//initial map file, try VRAM is paging or triple buffering
	MapFreeMem();
	
}

bool LoadLevel()
{
	//starts the level
	//does not perform resetting of all data as may be just a new life
	CleanUp();
	
	Player=new Car();

	int loaded=1; //default to not loaded
	
	//sort out levels later, for now just one
	//determine if we should load up into VRAM or standard memory
	//mappy doesn't support system bitmaps
	if(GameConfiguration->CapsActualSystem.GraphicsMode==MODE_PAGED || GameConfiguration->CapsActualSystem.GraphicsMode==MODE_TRIPLE)
	{
		loaded=MapLoadVRAM("transam.fmp");
		if(loaded!=0) Configuration::LogEntry("Buffer is VRAM but could not load map into vram, using standard");
		else Configuration::LogEntry("loaded map into VRAM");
	}
	if(loaded!=0)
	{
		loaded=MapLoad ("transam.fmp");
		if(loaded!=0) Configuration::LogEntry("Buffer is not VRAM but could not load map into memory! crikey!");
		else Configuration::LogEntry("loaded map into memory");
	}
	if(loaded!=0) return false;
	
	//mappy initialisation
	MapGenerateYLookup();
	MapInitAnims ();
	
	return true;	
}

